ImageGear Java PDF
Create a PDF from an Image

To create a PDF document from an image:

  1. Read the specified image.
  2. Get the image width, height, and resolution.
  3. Calculate the desired resulting size of the image to be added to the page (using the image width, height, and resolution).
  4. Present the image data as a memory byte array.
  5. Use the createDocument method of the PDF class to create a new instance of the Document class.
  6. Use the insertBlankPage method of the Document class to add a new blank page with the same size as the resulting image to the PDF document.
  7. Use the getPage method of the Document class to retrieve the newly added page of the PDF document.
  8. Create a new instance of the AddImageOptions class and provide the following data to its members:
    • setX(0)
    • setY(resultingHeight)
    • setWidth(resultingWidth)
    • setHeight(resultingHeight)
  9. Use the addImage methods of the Page class to add an image from a memory byte array to the PDF page.
  10. Use the close method of the Page class to close the page if you do not need it anymore.
  11. Use the saveDocument method of the Document class to save the resulting PDF document to a file.
  12. Use the close method of the Document class to close the document if you do not need it anymore.

The following is an illustration of how to create PDF page from an image:

 
Copy Code
import com.accusoft.imagegearpdf.*;

import java.awt.image.BufferedImage; 
import java.io.*;
import javax.imageio.ImageIO;

class PdfDemo
{
        private PDF pdf;
        private Document document;

        // Create a single-page PDF document from an image.
        public boolean createPdfPageFromImage(BufferedImage bufferedImage, double resultingWidth, double resultingHeight)
        {
                Page page = null;

                try
                {
                         // Write the image to a buffer.
                         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                         if (!ImageIO.write(bufferedImage, "png", outputStream))
                         {
                                   // The source image could not be written as a PNG image stream.
                                   return false;
                         }
                
                         // Present the image data as a memory byte array.
                         byte[] imageData = outputStream.toByteArray();

                         // Create a new document with a blank page having corresponding width and height.
                         document = pdf.createDocument();
                         document.insertBlankPage(0, resultingWidth, resultingHeight);

                         // Get the page to add image to.
                         page = document.getPage(0);

                         // Prepare the image options.
                         // Autosize cannot be used in this sample. Autosize calculates the height of the image internally, yet you must have the height of the image to set the Y coordinate.
                         AddImageOptions imageOptions = new AddImageOptions();
                         imageOptions.setX(0);
                         imageOptions.setY(resultingHeight);
                         imageOptions.setWidth(resultingWidth);
                         imageOptions.setHeight(resultingHeight);

                         // Add an image to the page.
                         page.addImage(imageData, imageOptions);

                         return true;
                  }
                  catch (Throwable ex)
                  {
                         // Failed to add an image to PDF page.
                         System.err.println("Exception: " + ex.toString());
                         return false;
                  }
                  finally
                  {
                         if (page != null)
                         {
                                  // Close PDF page as it is not needed anymore.
                                  page.close();
                         }
                  }
         }
}

See Also

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback